﻿using ChunQiu.Arch.Session.Core.ConfigSection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Framework.Zhaogang.Session.Core.SessionIDManagerStrategy
{
    /// <summary>
    /// 加密SessionID管理类
    /// </summary>
    public sealed class EncryptedSessionIDManager : SessionIDManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override string CreateSessionID(HttpContext context)
        {
            return string.Format("{0}.{1}", SessionProviderSettings.GetSettings().SessionProfix, base.CreateSessionID(context));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override string Encode(string id)
        {
            return DESEncryptor.Encode(id, SessionProviderSettings.GetSettings().DesKey, SessionProviderSettings.GetSettings().DesEncoding);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override string Decode(string id)
        {
            return DESEncryptor.Decode(id, SessionProviderSettings.GetSettings().DesKey, SessionProviderSettings.GetSettings().DesEncoding);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override bool Validate(string id)
        {
            string prefix;
            string realId;

            if (!Helper.ParseSessionID(id, out prefix, out realId))
                return false;

            return base.Validate(realId);
        }
    }
}
